<?php
/**
 * 【重要】路由器
 * Enter description here ...
 * @author hxm
 *
 */
class CRewriteUrl {
	
	public static $urlManager = null;
	
	public static $HttpHost   = null;
	
	public static $defaultAction = 'index';
	
	public static $callAction = null;
	
	public static $viewPath   = '.html';
	
	public function __construct($config) {
		//获取call config
		self::$urlManager = $config['urlManager'];
	}
	
	/**
	 * 解析路由
	 * Enter description here ...
	 * @param unknown_type $config
	 * @throws Exception
	 */
	public static function route() {
		if(self::$urlManager) {	
			switch (self::$urlManager) {
				case self::$urlManager['urlFormat'] == 'path':
					return self::UrlPath();
					break;
				case self::$urlManager['urlFormat'] == 'get':
					return self::UrlGet();
					break;
				default:
					throw new Exception('Routing is not supported');
			}
		} else {
			throw new Exception('Routing is not supported');
		}
	}
	
	/**
	 * http://www.xxx.com/index.php/?action=con&&function=fun
	 * Enter description here ...
	 */
	public static function UrlGet() {
		die('正在设计开发中');
	}
	
	/**
	 * http://www.xx.com/index.php/Con/funciton
	 * Enter description here ...
	 */
	public static function UrlPath() {
		//判断 index.php/index/index
		$strcont = strrpos($_SERVER['REQUEST_URI'],self::$defaultAction.'.php');
		if($strcont) {
			//index.php/xxx/xxx
			$urlManager = $_SERVER['PATH_INFO'];
		} else {
			//===/xxx/xxx
			$urlManager = $_SERVER['REQUEST_URI'];
		}
		///list-1.html
		if(strrpos($urlManager,'-')) {
			$urlRout = SoftBase::$config['urlManager']['rules'];
			self::parseUrl($urlRout,trim($_SERVER['REQUEST_URI'],'.html'));
		}
		//die;
		//生成出来
		//controllers/function
		$rou = array_filter(explode('/', $urlManager));
		$urlManager =  count($rou) == 1 ? $urlManager.'/'.self::$defaultAction : $urlManager;
		$controllers = trim($urlManager,'/') == '' ? '/'.self::$defaultAction.'/'.self::$defaultAction : $urlManager;
		/**
		 * array(
		 * 	'0' ==> $controllers
		 * 	'1' ==> $function
		 * )
		 */
		return array_filter(explode('/', $controllers));
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $manager
	 * @param unknown_type $request
	 */
	public static function parseUrl($manager,$request) {
		foreach($manager as $key=>$val) {
			echo $key.'<br>';
		}
		/*$request = trim($request,'/'.'/');
		preg_match('/^list-(?P\d+)\/$/u',$request,$matches);
		echo "<pre>";
		print_r($matches);die;*/
	}
}